package service

import (
	"context"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"time"
)

type _financialStatistics struct {
}

var FinancialStatistics = new(_financialStatistics)

func (s _financialStatistics) CreateApi(ctx context.Context, data *model.FinancialStatisticsEntity) (err error) {
	return dao.FinancialStatisticsEntity.WithContext(ctx).Create(data)
}

func (s _financialStatistics) GetIncomeApi(ctx context.Context, enterpriseID int64, startDate time.Time, endDate time.Time, customerID int64) (resp []*model.FinancialStatisticsEntity, err error) {
	tx := dao.FinancialStatisticsEntity.WithContext(ctx).Where(
		dao.FinancialStatisticsEntity.EnterpriseID.Eq(enterpriseID),
		dao.FinancialStatisticsEntity.CreatedAt.Gte(startDate),
		dao.FinancialStatisticsEntity.CreatedAt.Lte(endDate),
		dao.FinancialStatisticsEntity.Type.Eq(1),
	)
	if customerID > 0 {
		tx.Where(dao.FinancialStatisticsEntity.CustomerID.Eq(customerID))
	}
	return tx.Find()
}

func (s _financialStatistics) GetExpensesApi(ctx context.Context, enterpriseID int64, startDate time.Time, endDate time.Time, supplierID int64) (resp []*model.FinancialStatisticsEntity, err error) {
	tx := dao.FinancialStatisticsEntity.WithContext(ctx).Where(
		dao.FinancialStatisticsEntity.EnterpriseID.Eq(enterpriseID),
		dao.FinancialStatisticsEntity.CreatedAt.Gte(startDate),
		dao.FinancialStatisticsEntity.CreatedAt.Lte(endDate),
		dao.FinancialStatisticsEntity.Type.Eq(2),
	)
	if supplierID > 0 {
		tx.Where(dao.FinancialStatisticsEntity.SupplierID.Eq(supplierID))
	}
	return tx.Find()
}
